package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"math"
	"time"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Simple Window")

	label := widget.NewLabel("Hello, World!")
	button := widget.NewButton("Click me!", buttonClick(label))
	// 设置按钮的位置和大小
	button.Resize(fyne.NewSize(100, 10))
	button.Move(fyne.NewPos(300, 400))

	content := container.NewVBox(
		label,
	)
	// 创建一个布局容器，并将按钮添加到容器中
	buttonContainer := fyne.NewContainerWithLayout(layout.NewHBoxLayout(),
		layout.NewSpacer(),
		button,
		layout.NewSpacer(),
	)

	// 创建一个主布局容器，将内容容器和按钮容器添加到主容器中
	mainContainer := container.NewVBox(
		content,
		buttonContainer,
	)

	// 设置窗口大小为 600x600
	myWindow.Resize(fyne.NewSize(600, 600))
	myWindow.SetContent(mainContainer)
	myWindow.ShowAndRun()
}

func buttonClick(label *widget.Label) func() {
	return func() {
		label.SetText("Button clicked!")
		CalculateAngle()
		DoJson()
		//测试map
		testMap()
		//测试Chan
		//result, err := testChan([]interface{}{interface{}(1), interface{}(2), interface{}(3)})
		//_, _ = result, err

		main111()
	}
}
func CalculateAngleAndRadian(x1, y1, x2, y2 float64) (float64, float64) {
	deltaX := x2 - x1
	deltaY := y2 - y1

	radian := math.Atan2(deltaY, deltaX)
	angle := radian * (180 / math.Pi)

	if angle < 0 {
		angle += 360
	}
	if radian < 0 {
		radian += 2 * math.Pi
	}

	return angle, radian
}

func CalculateAngle() {
	//x1, y1 := 2844.0, 1039.0
	//x2, y2 := 2912.0, 800.0
	x1, y1 := 2912.0, 800.0
	x2, y2 := 2844.0, 1039.0

	angle, radian := CalculateAngleAndRadian(x1, y1, x2, y2)

	fmt.Println("角度:", angle)
	fmt.Println("弧度:", radian)
}
func testMap() {
	mapTIDs := make(map[uint32]uint64)
	mapTIDs[200] = 1
	mapTIDs[200] += 22
	mapTIDs[100] += 1
	mapTIDs[101] += 1
	mapTIDs[103] += 1
	mapTIDs[104] += 1
	mapTIDs[105] += 1
}
func testChan(args []interface{}) (result []interface{}, err error) {
	_ = args
	// 创建通道，用于接收异步结果
	resultChan := make(chan []interface{})
	//errChan := make(chan error)

	// 在 goroutine 中执行异步操作
	go func() {
		// 异步操作的逻辑
		// ...
		// 模拟异步操作，等待10秒
		time.Sleep(10 * time.Second)
		// 异步操作完成后，将结果发送到通道
		result := []interface{}{"异步操作的结果"}
		resultChan <- result
	}()

	// 在另一个 goroutine 中等待异步结果并进行处理
	//go func() {
	//	select {
	//	case res := <-resultChan:
	//		// 接收到异步结果
	//		result = res
	//	case err := <-errChan:
	//		// 接收到错误信息
	//		err = err
	//	}
	//}()
	// 使用 select 和 time.After 等待异步结果或超时
	select {
	case res := <-resultChan:
		// 接收到异步结果
		result = res
	case <-time.After(30 * time.Second):
		// 超过10秒，返回错误
		err = errors.New("异步操作超时")
	}

	// 返回通道，允许异步结果在后台处理
	return nil, nil
}

type BehaviorTree struct {
	Name  string         `json:"name"`
	Type  string         `json:"type"`
	Child []BehaviorNode `json:"child"`
}

type BehaviorNode struct {
	Name    string                 `json:"name"`
	Type    string                 `json:"type"`
	Child   []BehaviorNode         `json:"child,omitempty"`
	Ability string                 `json:"ability,omitempty"`
	Param   map[string]interface{} `json:"param,omitempty"`
	//Param *json.RawMessage `json:"param,omitempty"`
}

func printNode(node BehaviorNode, indent string) {
	fmt.Printf("%sName: %s\n", indent, node.Name)
	fmt.Printf("%sType: %s\n", indent, node.Type)
	if node.Ability != "" {
		fmt.Printf("%sAbility: %s\n", indent, node.Ability)
	}
	if len(node.Param) > 0 {
		fmt.Printf("%sParams:\n", indent)
		for key, value := range node.Param {
			fmt.Printf("%s  %s: %v\n", indent, key, value)
		}
	}
	fmt.Println()

	for _, child := range node.Child {
		printNode(child, indent+"  ")
	}
}

func main111() {
	jsonData := `{
		"name": "AI逻辑",
		"type": "selector",
		"child": [
			{
				"name": "攻击",
				"type": "sequence",
				"child": [
					{
						"name": "锁敌",
						"type": "action",
						"ability": "lock_target",
						"param": {}
					},
					{
						"name": "移动",
						"type": "action",
						"ability": "npc_teleport",
						"param": {
							"1": 100,
							"2": 100
						}
					}
				]
			}
		]
	}`

	var tree BehaviorTree
	err := json.Unmarshal([]byte(jsonData), &tree)
	if err != nil {
		fmt.Println("JSON unmarshal error:", err)
		return
	}

	fmt.Printf("Name: %s\n", tree.Name)
	fmt.Printf("Type: %s\n", tree.Type)
	fmt.Println("Child nodes:")
	for _, node := range tree.Child {
		printNode(node, "  ")
	}
}
